package com.example.pushdeom.UIActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.pushdeom.Bean.AppSharedPreferences;
import com.example.pushdeom.Bean.BaseApiListener;
import com.example.pushdeom.Bean.BaseUiListener;
import com.example.pushdeom.R;
import com.example.pushdeom.Tools.FontIconView;
import com.tencent.connect.UserInfo;
import com.tencent.connect.common.Constants;
import com.tencent.open.utils.HttpUtils;
import com.tencent.tauth.IRequestListener;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_qqlogin)
    FontIconView tvQqlogin;
    private Tencent mTencent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (hasRightAccount()) {
            startMainActivity();
        }
        setContentView(R.layout.activity_login2);
        ButterKnife.bind(this);
        if (mTencent==null) {
            mTencent = Tencent.createInstance("101919484", this.getApplicationContext());
        }
    }

    @OnClick({R.id.btn_login, R.id.tv_qqlogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                Toast.makeText(getApplicationContext(), "点击下面", Toast.LENGTH_LONG).show();
                break;
            case R.id.tv_qqlogin:
                if (!mTencent.isSessionValid()) {
                    mTencent.login(this, "all", new BaseUiListener(getApplicationContext(),"",mTencent));
                }
                break;
        }
    }
    private boolean hasRightAccount() {
        if (!AppSharedPreferences.getPreference(getApplicationContext(), "openid", "").equals("")){
            return true;
        }
        return false;
    }
    private void startMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, new BaseUiListener(getApplicationContext(),"",mTencent));
        if (requestCode == Constants.REQUEST_LOGIN) {
            startMainActivity();
        }
    }
}
